home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 316.adf / Vectors / V1.1 / src / polyvectors.c < prev    next >
C/C++ Source or Header  |  1990-02-06  |  8KB  |  269 lines

  1. /*                                                                  */
  2. /* Vectors - A quick test to see how fast Amigas draw lines         */
  3. /* Written by Gauthier Groult, May 89                               */
  4. /*                                                                  */
  5. /* Gauthier Groult, 33, Blvd Saint Denis, 92400 Courbevoie, France  */
  6. /* Tel: (1) 47 89 09 54, Email: ...inria!litp!germinal!groult       */
  7. /*                                                                  */
  8. /* Last updates:                                                    */
  9. /*    v1.00, 89.0518, first release, -ghg-                          */
  10. /*    v1.01, modified for speed by Jean-Michel Forgeas, June 89     */
  11. /*                 from 2000 to 2400 vectors/second                 */
  12. /*                 in a 704x280, 1 bit-plane BitMap                 */
  13. /*    v1.02, modified for speed by Jean-Michel Forgeas, June 89     */
  14. /*                 from 2000 to 2430 vectors/second                 */
  15. /*                 by calling XPolyLines()                          */
  16. /*                                                                  */
  17.  
  18. #include <intuition/intuisup.h>
  19. #include <intuition/intuitionbase.h>
  20. #include <graphics/gfxbase.h>
  21. #include <proto/graphics.h>
  22.  
  23. #define MAXW      1024
  24. #define MAXH      1024
  25. #define MINW      20
  26. #define MINH      20
  27. #define MIND      1
  28. #define MAXD      5
  29. #define RXOFFSET  0
  30.  
  31. #define PASSES    10
  32.  
  33. extern VOID __asm XClear( register __a0 struct RastPort *a );
  34.  
  35. extern VOID __asm XPolyLines( register __a0 struct RastPort *a,   /* rastport     */
  36.                               register __a4 UBYTE           *b,   /* start adress */
  37.                               register __d7 ULONG            c ); /* vectors num  */
  38.  
  39. struct GfxBase        *GfxBase;
  40. struct IntuitionBase  *IntuitionBase;
  41.  
  42. static UBYTE err_msg[10][40] =
  43.    {
  44.    "bad value for width, height or depth",
  45.    "cannot open required libraries",
  46.    "cannot start CIA timer",
  47.    "cannot get workbench screen data",
  48.    "not enough memory for graphics",
  49.    "no memory for the color map (!!)",
  50.    "incompatible depth/resolution",
  51.    "not enough memory for vectors",
  52.    };
  53.  
  54. static USHORT *BaseTab;  /* order: x1, y1, x2, y2, color, x1, etc... */
  55. static ULONG  vectors;
  56. static ULONG  s1[PASSES], m1[PASSES], s2[PASSES], m2[PASSES];
  57.  
  58. static struct View  *v, *oldview;
  59. static struct ViewPort  *vp;
  60. static struct RasInfo   *ri;
  61. static struct RastPort  *rp;
  62.  
  63. static USHORT  colortable[] =
  64.    {
  65.    0x000, 0x080, 0x190, 0x2A0, 0x3B0, 0x5D0, 0x6E0, 0x8F0,
  66.    0xE52, 0xE62, 0xE71, 0xE81, 0xEA1, 0xFB1, 0xFC0, 0x8F0,
  67.    0xC04, 0xC16, 0xD27, 0xD48, 0xE59, 0xE7B, 0xF8C, 0xFAD,
  68.    0x00F, 0x02F, 0x04E, 0x06E, 0x08E, 0x0AD, 0x0CD, 0x0DD
  69.    };
  70.  
  71. VOID Cleanup(error)
  72. UBYTE error;
  73. {
  74.    /* Free vector tables     */
  75.    if (BaseTab) Delete(BaseTab, vectors*PASSES*5);
  76.  
  77.    /* free graphics structures   */
  78.    if (rp)     FreeBMapRPort(rp);
  79.    if (ri)     Delete(ri, 1);
  80.    if (vp)
  81.       {
  82.       if (vp->ColorMap) FreeColorMap(vp->ColorMap);
  83.       FreeVPortCopLists(vp);
  84.       Delete(vp, 1);
  85.       }
  86.    if (v)
  87.       {
  88.       if (v->LOFCprList) FreeCprList(v->LOFCprList);
  89.       if (v->SHFCprList) FreeCprList(v->SHFCprList);
  90.       Delete(v, 1);
  91.       }
  92.  
  93.    if (error) printf("Vectors: %s - abort\n", err_msg[error-30]);
  94.  
  95.    /* terminate the timer    */
  96.    EndCIATimer();
  97.  
  98.    /* and close the libraries    */
  99.    if (GfxBase)         CloseLibrary(GfxBase);
  100.    if (IntuitionBase)   CloseLibrary(IntuitionBase);
  101.    exit(0);
  102. }
  103.  
  104.  
  105. VOID main(argc, argv)
  106. UBYTE argc, **argv;
  107. {
  108.    extern   struct ColorMap *GetColorMap();
  109.  
  110.    register int p, i;
  111.    int offset;
  112.  
  113.    ULONG    seconds, micros, sigma, width, height, depth;
  114.    USHORT   i_height, i_width;
  115.    struct   Screen wbScrData;
  116.  
  117.    /* get the command line args       */
  118.    if (argc!=5)
  119.       {
  120.       printf("Usage: Vectors <width> <height> <depth> <lines>\n");
  121.       printf("       Width, height and depth specify the viewport\n");
  122.       printf("       size and the number of colors, lines is the\n");
  123.       printf("       number of lines to draw.\n");
  124.       printf("       Use 0 as width and height for WorkBench size.\n");
  125.       printf("       Hires, Lace & Overscan are supported, Ham is not.\n");
  126.       printf("Vectors v1.00, by Gauthier Groult, 89.0518, public domain\n");
  127.       exit(10);
  128.       }
  129.    stcd_i(argv[1], &width);
  130.    stcd_i(argv[2], &height);
  131.    stcd_i(argv[3], &depth);
  132.    stcd_i(argv[4], &vectors);
  133.  
  134.    /* open the libraries          */
  135.    if (  !(GfxBase       = (struct GfxBase *)OpenLibrary("graphics.library", 0))
  136.       || !(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0))) Cleanup(31);
  137.  
  138.    /* init the microsec timer         */
  139.    if (!BeginCIATimer()) Cleanup(32);
  140.  
  141.    /* get workbench screen data       */
  142.    if (!GetScreenData(&wbScrData, sizeof(struct Screen), WBENCHSCREEN, NULL)) Cleanup(33);
  143.    i_width  = wbScrData.Width;
  144.    i_height = wbScrData.Height;
  145.    if (width==0)  width = i_width;
  146.    if (height==0) height = i_height;
  147.  
  148.    /* do some checking...         */
  149.    if (  width  < MINW || width  > MAXW
  150.       || height < MINH || height > MAXH
  151.       || depth  < MIND || depth  > MAXD ) Cleanup(30);
  152.  
  153.    /* get a pointer to the current view   */
  154.    oldview = GfxBase->ActiView;
  155.  
  156.    /* allocate our graphics strutures     */
  157.    if (  !(v  = New(struct View, 1))
  158.       || !(vp = New(struct ViewPort, 1))
  159.       || !(ri = New(struct RasInfo, 1))
  160.       || !(rp = AllocBMapRPort((UBYTE)depth, (USHORT)width, (USHORT)height))
  161.       )
  162.       Cleanup(34);
  163.  
  164.    /* Init the graphics engine        */
  165.    InitView(v);
  166.    v->DxOffset = IntuitionBase->ViewLord.DxOffset;
  167.    v->DyOffset = IntuitionBase->ViewLord.DyOffset;
  168.    v->ViewPort = vp;
  169.  
  170.    InitVPort(vp);
  171.    vp->RasInfo = ri;
  172.    vp->DWidth  = width;
  173.    vp->DHeight = height;
  174.    if(!(vp->ColorMap = GetColorMap(2<<depth))) Cleanup(35);
  175.    LoadRGB4(vp, colortable, 2<<depth);
  176.  
  177.    /* Set modes according to width/height */
  178.    if (width  > i_width/2) { if (depth>4) Cleanup(36); vp->Modes |= HIRES; }
  179.    if (height > i_height)  { v->Modes  |= LACE; vp->Modes |= LACE; }
  180.  
  181.    /* Horizontal centering on the display */
  182.    if (width > i_width/2) vp->DxOffset  = (i_width - width)/2;
  183.    else           vp->DxOffset  = (i_width/2 - width)/2;
  184.    /* Vertical centering on the display   */
  185.    if (height > i_height) vp->DyOffset  = (i_height*2 - height)/2;
  186.    else           vp->DyOffset  = (i_height - height)/2;
  187.  
  188.    ri->BitMap   = rp->BitMap;
  189.    ri->RxOffset = RXOFFSET;
  190.  
  191.    /* Init random number genarator  */
  192.    CurrentTime(&seconds, µs);
  193.    RandomSeed(micros);
  194.  
  195.    /* Build vector tables         */
  196.    if (!(BaseTab=New(USHORT, vectors*PASSES*5))) Cleanup(37);
  197.  
  198.    printf("Building vector tables... ");
  199.    for(p=0; p<PASSES; p++)
  200.       {
  201.       printf("%ld", p+1);
  202.       offset = p*vectors*5;
  203.       for(i=0; i<vectors; i++)
  204.          {
  205.          BaseTab[offset++] = Random(width);
  206.          BaseTab[offset++] = Random(height);
  207.          BaseTab[offset++] = Random(width);
  208.          BaseTab[offset++] = Random(height);
  209.          BaseTab[offset++] = Random((2<<depth)-1)+1;
  210.          }
  211.       printf("\b");
  212.       if (p>8) printf("\b");
  213.       if (p>98) printf("\b");
  214.       }
  215.    printf("Done\n");
  216.  
  217.    /* ... and draw the lines!     */
  218.    MakeVPort(v, vp);
  219.    MrgCop(v);
  220.    LoadView(v);
  221.    p=0;
  222.    OwnBlitter();
  223.    Forbid();
  224.    while (p<PASSES)
  225.       {
  226.       offset = (int)BaseTab + p * vectors * 10;
  227.       XClear( rp );
  228.       ElapsedTime(&s1[p], &m1[p]);
  229.       XPolyLines( rp, (UBYTE*)offset, vectors );
  230.       ElapsedTime(&s2[p], &m2[p]);
  231.       p++;
  232.       }
  233.    Permit();
  234.    DisownBlitter();
  235.    /* restore original view       */
  236.    if (oldview) LoadView(oldview);
  237.  
  238.    /* print time table            */
  239.    sigma = 0;
  240.    for(p=0; p<PASSES; p++)
  241.       {
  242.       i = 0;
  243.       while (s2[p]>s1[p])
  244.         {
  245.         m2[p] += 1000000;
  246.         s2[p]--;
  247.         }
  248.  
  249.       sigma += m2[p]-m1[p];
  250.       while (m2[p]-m1[p]>1000000)
  251.         {
  252.         m2[p]-=1000000;
  253.         i++;
  254.         }
  255.  
  256.       printf("pass %ld:  %ld\" %ld µs\n", p, i, m2[p]-m1[p]);
  257.       }
  258.  
  259.       i=0;
  260.       sigma = sigma/PASSES;
  261.       while (sigma>1000000)
  262.         {
  263.         sigma-=1000000;
  264.         i++;
  265.         }
  266.       printf("average: %ld\" %ld µs\n", i, sigma);
  267.  
  268.    Cleanup(0);
  269. }